package newHomework;
import java.util.Scanner;

public class Next07 {
    //设置全局变量
    static String[] name = new String[3];//商品名字
    static double[] price = new double[3];//商品价格
    static int[] number = new int[3];//商品数量
    static int count = 0;


    public static void main(String[] args) {
        /**
         * 写一个程序，实现商品录入系统
         * 通过java基础的相关知识，设计并完成一个简单的商品录入系统，可以实现：
         * 菜单显示、商品信息展示、商品信息录入、商品信息查询、退出的功能。
         */
        for (; ; ) {
            int num = meau();
            switch (num) {
                case 1:
                    show();
                    break;//显示商品信息
                case 2:
                    add();
                    break;//添加商品
                case 3:
                    inquiry();
                    break;
                case 4:
                    information();
                    break;//统计商品
                case 5:
                    return;//退出
                default:
                    System.out.println("输入不准确");
            }
        }
    }

    public static int meau() {//菜单目录
        System.out.println("根据输入的数字1到5来选择功能");
        System.out.println("* 1. 商品列表");
        System.out.println("* 2. 商品录入");
        System.out.println("* 3. 商品查询");
        System.out.println("* 4. 统计信息");
        System.out.println("* 5. 退出");
        if (count == 0) {
            System.out.println("还没有录入商品");

        }
        System.out.println("请输入数字选择功能");
        return new Scanner(System.in).nextInt();
    }

    public static void show() {
        if (count == 0) {
            System.out.println("当前没有商品信息");
        } else {
            System.out.println("商品名字\t\t\t价格\t\t\t数量");
            for (int i = 0; i < name.length; i++) {//循环输出商品信息
                System.out.print(name[i]+"\t\t\t");
                System.out.print(price[i]+"\t\t\t");
                System.out.print(number[i]);
                System.out.println();
            }
        }
    }

    // 默认两倍扩容
    public static void AddItem(String productName, double productPrice, int productNumber) {
        System.out.printf("count  = %d\n", count);
        if (count == name.length - 1) {
            String[] newName = new String[(count + 1) * 2];
            double[] newPrice = new double[(count + 1) * 2];
            int[] newNumber = new int[(count + 1) * 2];
            for (int i = 0; i < count; i++) {
                newName[i] = name[i];
                newPrice[i] = price[i];
                newNumber[i] = number[i];
            }
            name = newName;
            price = newPrice;
            number = newNumber;
        }
        name[count] = productName;
        price[count] = productPrice;
        number[count] = productNumber;
        count++;
    }

    public static void add() {
        String prodcutName = "";
        double productPrice = 0;
        int productNumber = 0;
        for (; ; ) {
            //给每个商品输入信息
            System.out.println("请输入第" + (count + 1) + "件商品的信息");
            prodcutName = new Scanner(System.in).nextLine();
            System.out.println("请输入第" + (count + 1) + "件商品的价格");
            productPrice = new Scanner(System.in).nextDouble();
            System.out.println("请输入第" + (count + 1) + "件商品的数量");
            productNumber = new Scanner(System.in).nextInt();
            AddItem(prodcutName, productPrice, productNumber);
            System.out.println("添加成功");
            System.out.println("是否继续添加商品?(y是的 n退出)");
            String input = new Scanner(System.in).nextLine();
            if (input.equals("y")) {
                continue;
            } else if (input.equals("n")) {
                show();//显示结果
                break;
            } else {
                System.out.println("输入错误");
                break;
            }
        }
    }

    public static void inquiry() {
        if (count == 0) {
            System.out.println("还没有添加商品");
            return;//返回菜单
        }
        System.out.println("输入要查询的信息");
        String input = new Scanner(System.in).nextLine();
        for (int i = 0; i < name.length; i++) {
            if (input.equals(name[i])) {
                System.out.printf("商品是" + name[i] + "\t" + "价格" + price[i] + "元" + "\t" + "数量" + number[i] + "\n");
                break;
            } else {
                System.out.println("没有找到信息");

            }
        }
    }

    public static void information() {
        /**功能有平均
         *统计信息的方法需要输出，商品总价是多少，单价均价是多少，最高单价是多少，最高总价是多少
         */
        double maxSum = 0;
        double sum = 0;
        double avg = 0;
        double max = price[0];
        if (count == 0) {
            System.out.println("当前还没有商品,请先完成添加");
            return;//返回菜单
        }
        System.out.println("每种总价" + "\t\t\t" + "单价均价" + "\t\t\t" + "最高价单价" + "\t\t\t" + "最高总价");

        for (int i = 0; i <= price.length; i++) {
            sum += price[i];

        }
        avg =maxSum/ name.length;//平均价

        for (int i = 0; i < name.length; i++) {
            if (max < price[i]) {
                max = price[i];
            }
        }
        //最高总价位 =(单价*件数)*种类
        for (int i = 0; i < count; i++) {
            maxSum += price[i] * number[i];
        }
        System.out.println(sum + "\t\t\t" + avg + "\t\t\t" + max + "\t\t\t" + maxSum);
    }
}
